﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SecurityApi.DataAccess
{
    public class ResourceRepository : BaseRepository
    {
        #region Resource related
        public IQueryable<Resource> GetResources(int applicationId = 1)
        {
            var query = from o in _context.Resources
                        where o.ApplicationId == applicationId
                        select o;
            return query;
        }

        public Resource GetResourceById(int resourceId)
        {
            var query = from o in _context.Resources
                        where o.ResourceId == resourceId
                        select o;
            return query.SingleOrDefault();
        }

        public Resource GetResource(string name, string uri, int applicationId = 1)
        {
            name = name.Trim().ToLower();
            uri = uri.Trim().ToLower();
            var query = _context.Resources.Where(p => p.Name == name && p.Uri == uri && p.ApplicationId == applicationId);
            return query.SingleOrDefault();
        }

        public bool AddResource(string name, string uri, int applicationId = 1)
        {
            name    = name.Trim().ToLower();
            uri     = uri.Trim().ToLower();

            bool bResult = true;

            try
            {
                Resource resource= new Resource();
                resource.Name = name;
                resource.Uri = uri;
                resource.ApplicationId = applicationId;
                resource.adt_DateCreated = DateTime.Now;
                _context.Resources.Add(resource);
                _context.SaveChanges();
            }
            catch (Exception ex)
            {
                bResult = false;
            }

            return bResult;
        }

        public bool RemoveResource(int resourceId)
        {
            bool bResult = true;

            try
            {
                Resource resource = _context.Resources.Where(p => p.ResourceId == resourceId).SingleOrDefault();

                if (null != resource)
                {
                    _context.Resources.Remove(resource);
                    _context.SaveChanges();
                }
                else
                {
                    bResult = false;
                }

            }
            catch (Exception ex)
            {
                bResult = false;
            }

            return bResult;
        }

        public bool RemoveResource(string name, string uri, int applicationId = 1)
        {
            bool bResult = true;
            name = name.Trim().ToLower();
            uri = uri.Trim().ToLower();
            try
            {
                Resource resource = _context.Resources.Where(p => p.Name == name && p.Uri == uri && p.ApplicationId == applicationId).SingleOrDefault();

                if (null != resource)
                {
                    _context.Resources.Remove(resource);
                    _context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                bResult = false;
            }

            return bResult;
        }

        #endregion
    }
}
